package compendiumAid.presentation.compendiumElements
{
	import compendiumAid.application.AddElementsToSelectionEvent;
	import compendiumAid.application.AlwaysViewElementEvent;
	import compendiumAid.application.ViewElementEvent;
	import compendiumAid.application.loadElementsList.LoadElementsListEvent;
	import compendiumAid.domain.ElementUtil;
	import compendiumAid.domain.model.ApplicationModel;
	import compendiumAid.domain.vo.ElementVO;
	import compendiumAid.presentation.PresentationModel;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;

	[ManagedEvents(names="AddElementsEvent, LoadElementsEvent, ViewElementEvent")]
	public class CompendiumElementsPM extends PresentationModel
	{
		[Inject]
		public var model:ApplicationModel;
		
		[Bindable]
		public var elementClassifications:ArrayCollection = new ArrayCollection(["Monster", "Item", "Ritual", "Trap", "Power"]);
		
		[Bindable]
		public var elements:ArrayCollection;
		
		private var alwaysView:Boolean = false;
		private var allElements:ArrayCollection = new ArrayCollection();;
		private var searchString:String;
		
		public function loadElements(classification:String):void
		{
			var eventClassification:String;
			
			switch (classification)
			{
				case "Monster" 	: eventClassification = LoadElementsListEvent.ELEMENT_CLASSIFICATION_MONSTER; 	break;
				case "Item" 	: eventClassification = LoadElementsListEvent.ELEMENT_CLASSIFICATION_ITEM; 		break;
				case "Ritual" 	: eventClassification = LoadElementsListEvent.ELEMENT_CLASSIFICATION_RITUAL; 	break;
				case "Trap" 	: eventClassification = LoadElementsListEvent.ELEMENT_CLASSIFICATION_TRAP; 		break;
				case "Power" 	: eventClassification = LoadElementsListEvent.ELEMENT_CLASSIFICATION_POWER;		break;
				default : break;
			}
			
			if (eventClassification != null)
			{
				dispatcher(new LoadElementsListEvent(eventClassification, allElements));
			}
		}
		
		public function addElementsToSelection(elementsVector:Vector.<Object>):void
		{
			var elementsArray:ArrayCollection = new ArrayCollection;
			
			for each (var elementObject:Object in elementsVector)
			{
				var elementVO:ElementVO = ElementUtil.elementObjectToElementVO(elementObject);
				
				if (elementVO != null)
				{
					elementsArray.addItemAt(elementVO, 0);
				}
			}
			
			dispatcher(new AddElementsToSelectionEvent(elementsArray));
		}
		
		public function search(searchString:String):void
		{
			if (allElements.length > 0)
			{
				if (searchString.length > 0)
				{
					this.searchString = searchString;
					var matches:Array = allElements.source.filter(findName);
					elements.source = matches;
				}
				else
				{
					elements = ObjectUtil.copy(allElements) as ArrayCollection;
				}
			}
		}
		
		public function viewElement(elementObject:Object):void
		{
			var elementVO:ElementVO = ElementUtil.elementObjectToElementVO(elementObject);
			dispatcher(new ViewElementEvent(elementVO));
		}
		
		public function selectElement(elementObject:Object):void
		{
			if (alwaysView == true)
			{
				viewElement(elementObject);
			}
		}
		
		[MessageHandler]
		public function alwaysViewElement(event:AlwaysViewElementEvent):void
		{
			alwaysView = event.alwaysView;
		}
		
		private function findName(elementVO:ElementVO, index:int, array:Array):Boolean
		{
			var name:String = (elementVO.name as String).toLowerCase();
			var search:String = searchString.toLowerCase();
			return (name.indexOf(search) >= 0);
		}
	}
}